﻿using System;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.Configuration;

namespace Connect
{
    public partial class Connection : Form
    {
        public Connection()
        {
            InitializeComponent();
        }
        private void frmConnection_Load(object sender, EventArgs e)
        {
            cmbAuthentication.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            cmbDatabase.Items.Clear();
            SqlConnection con;
            SqlDataReader reader;
            try
            {
                if (cmbAuthentication.SelectedIndex == 0)
                    con = new SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog=master;Integrated Security=True;");
                else
                    con = new SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog=master;User Id=" + txtUsername.Text + ";Password=" + txtPassword.Text + ";");
                SqlCommand cmd = new SqlCommand("SP_DATABASES", con);
                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    cmbDatabase.Items.Add(reader[0].ToString());
                }
                cmbDatabase.Enabled = true;
                MessageBox.Show("Kết nối thành công!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Dispose();
                cmd.Dispose();
            }
            catch
            {
                MessageBox.Show("Lỗi, vui lòng thử lại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAuthentication.SelectedIndex == 0)
            {
                txtUsername.Enabled = false;
                txtPassword.Enabled = false;
                label3.Enabled = false;
                label4.Enabled = false;
            }
            else
            {
                txtUsername.Enabled = true;
                txtPassword.Enabled = true;
                label3.Enabled = true;
                label4.Enabled = true;
            }
        }

        private void cmbDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            if (cmbAuthentication.SelectedIndex == 0)
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("QL_CuahangSach.exe");
                ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
                connectionStringsSection.ConnectionStrings["cnnString"].ConnectionString = "server=" + txtServer.Text + ";database=" + cmbDatabase.Text + ";Integrated Security=True;";
                config.Save();
            }
            else
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("QuanLyCuaHang.exe");
                ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
                connectionStringsSection.ConnectionStrings["cnnString"].ConnectionString = "server=" + txtServer.Text + ";database=" + cmbDatabase.Text + ";uid=" + txtUsername.Text + ";pwd=" + txtPassword.Text + ";";
                config.Save();
            }
            this.Close();
        }

       
    }
}